{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e8b27860",
   "metadata": {},
   "source": [
    "# What\n",
    "分类任务，支持两种模式\n",
    "1. Folder模式，需要输入`train`, `valid`两个测试集对应的目录。`labels.txt`，需要训练的label，里面每个类别一行。\n",
    "2. List模式，需要输入`train`, `valid`两个测试集对应的训练文件，每行一个样本。`labels.txt`是可选参数，里面每个类别一行。`data_pattern`一个通用的目录，与train、val中的第一列进行拼接。\n",
    "\n",
    "### 支持的模型名称\n",
    "\n",
    "模型名称替换代码中的 `model_name`变量的值。\n",
    "\n",
    "| **模型系列** | **模型名称**                                                 |\n",
    "| ------------ | ------------------------------------------------------------ |\n",
    "| ResNet       | resnet10, resnet18, resnet34, resnet50, resnet101, resnet152, resnet200 |\n",
    "| DenseNet     | DenseNet121, DenseNet169, DenseNet201, DenseNet264           |\n",
    "| ShuffleNet   | ShuffleNet |\n",
    "| Transformer     | ViT, SimpleViT         |\n",
    "| OnekeyAI | UNetClassification |             "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e861016",
   "metadata": {},
   "source": [
    "### List模式\n",
    "\n",
    "在Onekey中List模式一般是采用labelme标注出来的结果，如果要使用自己的数据应用List模式，需要根据自己的实际情况对数据进行处理。\n",
    "\n",
    "* `train.txt`，训练数据列表，中间用\\t（Tab水平制表符）进行分割。\n",
    "* `val.txt`，验证数据列表，中间用\\t（Tab水平制表符）进行分割。\n",
    "* `labels.txt`，label的集合，表明训练数据多少标签。\n",
    "* `data_pattern`参数，所有数据存在的目录的公共前缀，如果`train.txt`,`val.txt`文件里面存放的是绝对路径，`data_pattern`设置为None即可。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7050436a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from collections import namedtuple\n",
    "\n",
    "from onekey_algo.classification3d.run_classification3d import main as clf_main3d\n",
    "from onekey_algo import get_param_in_cwd\n",
    "# 设置参数\n",
    "roi_size = [96, 96, 96]\n",
    "vit_settings = {\n",
    "        'image_size': roi_size[-1],  # image size\n",
    "        'frames': roi_size[0],  # number of frames\n",
    "        'image_patch_size': 16,  # image patch size\n",
    "        'frame_patch_size': roi_size[0] // 4,  # frame patch size\n",
    "        'dim': 1024,\n",
    "        'depth': 6,\n",
    "        'heads': 8,\n",
    "        'mlp_dim': 2048,\n",
    "        'dropout': 0.1,\n",
    "        'emb_dropout': 0.1}\n",
    "\n",
    "# 模型存放在哪里\n",
    "model_root = 'models3d'\n",
    "# 你自己组合出来是几个通道\n",
    "in_channels = 4\n",
    "# 训练集文件\n",
    "train_file = r'C:/Users/onekey/Desktop/demo/train3d.txt'\n",
    "# 非训练集文件\n",
    "val_file = r'C:/Users/onekey/Desktop/demo/val3d.txt'\n",
    "# 3D 多通道数据存放的位置，nii.gz格式\n",
    "data_pattern = r'E:\\OnekeyDS\\multi_modal_omics\\CT\\i_c4'\n",
    "# 没有的话会自动创建\n",
    "labels_file = r'C:\\Users\\onekey\\Desktop\\demo/labels.txt'\n",
    "# 你喜欢的模型\n",
    "model_names = ['ShuffleNet', 'resnet50', 'resnet101', 'DenseNet121', ]\n",
    "for model_name in model_names:\n",
    "    params = dict(train=train_file,\n",
    "                  val=val_file,\n",
    "                  labels_file=labels_file,\n",
    "                  data_pattern=data_pattern,\n",
    "                  j=6,\n",
    "                  max2use=None,\n",
    "                  normalize_method='imagenet',\n",
    "                  model_name=model_name,\n",
    "                  gpus=[0],\n",
    "                  roi_size=roi_size,\n",
    "                  batch_size=4,\n",
    "                  epochs=96 if model_name in ['ViT', 'SimpleViT'] else 48,\n",
    "                  init_lr=0.01,\n",
    "                  optimizer='adam',\n",
    "                  retrain=None,\n",
    "                  model_root=model_root,\n",
    "                  val_interval=1,\n",
    "                  in_channels=in_channels,\n",
    "                  iters_verbose=1,\n",
    "                  model_config={'groups': 1, 'blocks_args': 'efficientnet-b4'},\n",
    "                  vit_settings=vit_settings,\n",
    "                  save_per_epoch=True,\n",
    "                  squeeze_dim=-2,\n",
    "                  pretrained=False)\n",
    "    # 训练模型\n",
    "    Args = namedtuple(\"Args\", params)\n",
    "    clf_main3d(Args(**params))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d936baa6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
